package cn.edu.leetcode.其他;

public class 实现strStr {

    public static void main(String[] args) {
        String haystack = "hello", needle = "ll";
        System.out.println(strStr(haystack, needle));
    }

    public static int strStr(String haystack, String needle) {
        char[] haystackChars = haystack.toCharArray();
        char[] needleChars = needle.toCharArray();

        int i,j;
        for (i = 0, j = 0; i < haystackChars.length && j < needleChars.length; i++) {
            if (haystackChars[i] == needleChars[j]) {
                j++;
            } else {
                i -= j;
                j = 0;
            }
        }

        if (j == needleChars.length){
            return i-j;
        }

        return -1;
    }

    public static int strStr2(String haystack, String needle) {
        return haystack.indexOf(needle);
    }
}
